package com.cisdi.info.simple.service.permission.impl;

import com.cisdi.info.simple.dao.permission.PermissionDao;
import com.cisdi.info.simple.dto.base.PageDTO;
import com.cisdi.info.simple.dto.base.PageResultDTO;
import com.cisdi.info.simple.entity.permission.Module;
import com.cisdi.info.simple.util.ModuleManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.cisdi.info.simple.entity.permission.Permission;
import com.cisdi.info.simple.service.base.BaseService;
import com.cisdi.info.simple.service.permission.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PermissionServiceBean extends BaseService implements PermissionService {

    private static Logger logger = LogManager.getLogger();

    @Autowired
    private PermissionDao permissionDao;

    public PageResultDTO findPermissions(PageDTO pageDTO) {
        pageDTO.setStartIndex((pageDTO.getCurrentPage() - 1) * pageDTO.getPageSize());
        List<Permission> permissionDTOS = this.permissionDao.findPermissions(pageDTO);
        Long totalCount = this.permissionDao.findPermissionTotalCount(pageDTO);

        PageResultDTO pageResultDTO = new PageResultDTO();
        pageResultDTO.setTotalCount(totalCount);
        pageResultDTO.setDatas(permissionDTOS);

        return pageResultDTO;
    }

    public List<Permission> findAllPermissions() {
        return this.permissionDao.findAllPermissions();
    }

    public List<Permission> findAllPermissionsWithIdName() {
        return this.permissionDao.findAllPermissionsWithIdName();
    }

    public Permission findPermission(Long permissionId) {
        return this.permissionDao.findPermission(permissionId);
    }

    //所有外键的Name都以加载
    public Permission findPermissionWithForeignName(Long permissionId) {
        return this.permissionDao.findPermissionWithForeignName(permissionId);
    }

    public Permission savePermission(Permission permission) {
        this.setSavePulicColumns(permission);
        return this.permissionDao.savePermission(permission);
    }

    public Permission updatePermission(Permission permission) {
        this.setUpdatePulicColumns(permission);
        return this.permissionDao.updatePermission(permission);
    }

    public void deletePermission(Long permissionId) {
        this.permissionDao.deletePermission(permissionId);
    }

    public List<Permission> getAllPermissions() {
        List<Permission> permissions = new ArrayList<Permission>();
        for (Module module : ModuleManager.getAllModules()) {
            permissions.addAll(module.getPermissions());
        }
        return permissions;
    }
}
